package com.lzb.compressmaster.fragment;

import android.view.View;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.lzb.compressmaster.BaseNoModelFragment;
import com.lzb.compressmaster.R;
import com.lzb.compressmaster.activity.GoCompressActivity;
import com.lzb.compressmaster.adapter.SelVideoAdapter;
import com.lzb.compressmaster.basic.picselect.MediaLoadManager;
import com.lzb.compressmaster.basic.picselect.PhotoConfig;
import com.lzb.compressmaster.basic.picselect.SelectMediaEntity;
import com.lzb.compressmaster.databinding.FragmentVideoBinding;
import com.lzb.compressmaster.utils.CompressFileManager;
import com.lzb.compressmaster.utils.RxUtil;

import java.util.List;

import io.reactivex.rxjava3.core.ObservableEmitter;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link VideoFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class VideoFragment extends BaseNoModelFragment<FragmentVideoBinding> implements OnItemClickListener {

    private CompressFileManager mInstance;
    private SelVideoAdapter videoAdapter;

    @Override
    protected int onCreate() {
        return R.layout.fragment_video;
    }

    @Override
    protected void initView() {
        mInstance = CompressFileManager.getInstance();

        mDataBinding.rvVideoList.setLayoutManager(new GridLayoutManager(mContext, 3));
        videoAdapter = new SelVideoAdapter();
        mDataBinding.rvVideoList.setAdapter(videoAdapter);
        videoAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        videoAdapter.getItem(position).setChecked(!videoAdapter.getItem(position).isChecked());
        videoAdapter.notifyDataSetChanged();
        boolean hasExist = false;
        int tmpPosition = -1;
        for (String selectPath : mInstance.getSelectFileBeans()) {
            tmpPosition++;
            if (videoAdapter.getItem(position).getPath().equals(selectPath)) {
                hasExist = true;
                break;
            }
        }
        if (hasExist) {
            mInstance.deleteSelectFile(tmpPosition);
        } else {
            mInstance.addSelectFile(videoAdapter.getItem(position).getPath());
        }
        ((GoCompressActivity) mContext).refreshDataCount(mInstance);
    }

    @Override
    protected void initData() {
        getData();
    }

    //获取视频和图片数据
    private void getData() {
        RxUtil.create(new RxUtil.Callback<List<SelectMediaEntity>>() {
            @Override
            public void doBackground(ObservableEmitter<List<SelectMediaEntity>> emitter) {
                List<SelectMediaEntity> listSys = MediaLoadManager.loadAllMediaData(mContext, PhotoConfig.Mode.VIDEO);
                emitter.onNext(listSys);
            }

            @Override
            public void accept(List<SelectMediaEntity> selectMediaEntities) {
                if (selectMediaEntities.size() > 0) {
                    videoAdapter.setList(selectMediaEntities);
                    mDataBinding.rvVideoList.setVisibility(View.VISIBLE);
                    mDataBinding.flVideoNotData.setVisibility(View.GONE);
                } else {
                    mDataBinding.rvVideoList.setVisibility(View.GONE);
                    mDataBinding.flVideoNotData.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}